<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Python连接MySQL | Introduction</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../home/part3/1.CRUD.html" />
    
    
    <link rel="prev" href="../../home/part2/7.慢查询及sql优化.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="4"
        data-chapter-title="Python连接MySQL"
        data-filepath="home/part3/Python连接MySQL.md"
        data-basepath="../.."
        data-revision="Sun Oct 21 2018 16:32:53 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="home/MySQL介绍.html">
            
                
                    <a href="../../home/MySQL介绍.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        MySQL介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="home/part1/MySQL基础.html">
            
                
                    <a href="../../home/part1/MySQL基础.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Mysql基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="home/part1/1.MySQL基础操作.html">
            
                
                    <a href="../../home/part1/1.MySQL基础操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        MySQL基础操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="home/part1/2.MySQL数据类型.html">
            
                
                    <a href="../../home/part1/2.MySQL数据类型.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        MySQL数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="home/part1/3.MySQL建库建表.html">
            
                
                    <a href="../../home/part1/3.MySQL建库建表.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        MySQL建库建表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="home/part1/4.DML操作：增,删,改.html">
            
                
                    <a href="../../home/part1/4.DML操作：增,删,改.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        DML操作：增,删,改
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="home/part1/5.DQL操作：数据查询.html">
            
                
                    <a href="../../home/part1/5.DQL操作：数据查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        DQL操作：数据查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="home/part1/6.数据导入和导出.html">
            
                
                    <a href="../../home/part1/6.数据导入和导出.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        数据导入和导出
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="home/part2/MySQL高级.html">
            
                
                    <a href="../../home/part2/MySQL高级.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        MySQL高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="home/part2/1.MySQL的多表联查.html">
            
                
                    <a href="../../home/part2/1.MySQL的多表联查.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        MySQL的多表联查
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="home/part2/2.MySQL的事务处理.html">
            
                
                    <a href="../../home/part2/2.MySQL的事务处理.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        MySQL的事务处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="home/part2/3.MySQL的视图及其它.html">
            
                
                    <a href="../../home/part2/3.MySQL的视图及其它.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        MySQL的视图及其它
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="home/part2/4.MySQL的触发器.html">
            
                
                    <a href="../../home/part2/4.MySQL的触发器.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        MySQL的触发器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="home/part2/6.MySQL日志及数据恢复.html">
            
                
                    <a href="../../home/part2/6.MySQL日志及数据恢复.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        MySQL日志及数据恢复
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="home/part2/7.慢查询及sql优化.html">
            
                
                    <a href="../../home/part2/7.慢查询及sql优化.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        慢查询及sql优化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="4" data-path="home/part3/Python连接MySQL.html">
            
                
                    <a href="../../home/part3/Python连接MySQL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        Python连接MySQL
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="home/part3/1.CRUD.html">
            
                
                    <a href="../../home/part3/1.CRUD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        CRUD
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Introduction</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="python3-mysql-&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;">Python3 MySQL &#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;</h1>
<hr>
<p>MySQL &#x53EF;&#x5E94;&#x7528;&#x4E8E;&#x591A;&#x79CD;&#x8BED;&#x8A00;&#xFF0C;&#x5305;&#x62EC; PERL, C, C++, JAVA &#x548C; PHP</p>
<ul>
<li><h3 id="&#x4EC0;&#x4E48;&#x662F;-pymysql&#xFF1F;">&#x4EC0;&#x4E48;&#x662F; PyMySQL&#xFF1F;</h3>
</li>
</ul>
<pre><code class="lang-sql">PyMySQL &#x662F;&#x5728; Python3.x &#x7248;&#x672C;&#x4E2D;&#x7528;&#x4E8E;&#x8FDE;&#x63A5; MySQL &#x670D;&#x52A1;&#x5668;&#x7684;&#x4E00;&#x4E2A;&#x5E93;&#xFF0C;Python2&#x4E2D;&#x5219;&#x4F7F;&#x7528;mysqldb&#x3002;

PyMySQL &#x9075;&#x5FAA; Python &#x6570;&#x636E;&#x5E93; API v2.0 &#x89C4;&#x8303;&#xFF0C;&#x5E76;&#x5305;&#x542B;&#x4E86; pure-Python MySQL &#x5BA2;&#x6237;&#x7AEF;&#x5E93;&#x3002;
</code></pre>
<ul>
<li><h3 id="pymysql-&#x5B89;&#x88C5;">PyMySQL &#x5B89;&#x88C5;</h3>
</li>
</ul>
<pre><code class="lang-bash">&#x5728;&#x4F7F;&#x7528; PyMySQL &#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x786E;&#x4FDD; PyMySQL &#x5DF2;&#x5B89;&#x88C5;&#x3002;

PyMySQL &#x4E0B;&#x8F7D;&#x5730;&#x5740;&#xFF1A;https://github.com/PyMySQL/PyMySQL&#x3002;

&#x5982;&#x679C;&#x8FD8;&#x672A;&#x5B89;&#x88C5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x5B89;&#x88C5;&#x6700;&#x65B0;&#x7248;&#x7684; PyMySQL&#xFF1A;

$ pip3 install PyMySQL
</code></pre>
<ul>
<li><h3 id="&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;">&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;</h3>
</li>
</ul>
<blockquote>
<p>&#x901A;&#x8FC7;&#x5982;&#x4E0B;&#x4EE3;&#x7801;&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;</p>
</blockquote>
<pre><code class="lang-python"> <span class="hljs-comment">#!/usr/bin/python3</span>

 <span class="hljs-keyword">import</span> pymysql

 <span class="hljs-comment"># &#x6253;&#x5F00;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;</span>
 db = pymysql.connect(<span class="hljs-string">&quot;localhost&quot;</span>,<span class="hljs-string">&quot;root&quot;</span>,<span class="hljs-string">&quot;123456&quot;</span>,<span class="hljs-string">&quot;mydb&quot;</span> )

 <span class="hljs-comment"># &#x4F7F;&#x7528; cursor() &#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6E38;&#x6807;&#x5BF9;&#x8C61; cursor</span>
 cursor = db.cursor()

 <span class="hljs-comment"># &#x4F7F;&#x7528; execute()  &#x65B9;&#x6CD5;&#x6267;&#x884C; SQL &#x67E5;&#x8BE2; </span>
 cursor.execute(<span class="hljs-string">&quot;SELECT VERSION()&quot;</span>)

 <span class="hljs-comment"># &#x4F7F;&#x7528; fetchone() &#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x5355;&#x6761;&#x6570;&#x636E;.</span>
 data = cursor.fetchone()

 <span class="hljs-keyword">print</span> (<span class="hljs-string">&quot;Database version : %s &quot;</span> % data)

 <span class="hljs-comment"># &#x5173;&#x95ED;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;</span>
 db.close()
</code></pre>
<h2 id="&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x53EF;&#x9009;&#x53C2;&#x6570;">&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x53EF;&#x9009;&#x53C2;&#x6570;</h2>
<pre><code class="lang-python">pymysql &#x94FE;&#x63A5;&#x6570;&#x636E;&#x5E93;

&#x53C2;&#x6570;<span class="hljs-number">1</span>&#xFF0C;mysql&#x670D;&#x52A1;&#x5668;&#x7684;&#x5730;&#x5740;  &#x53C2;&#x6570;<span class="hljs-number">2</span> &#x767B;&#x5F55;mysql&#x7684;&#x7528;&#x6237;&#x540D;  &#x53C2;&#x6570;<span class="hljs-number">3</span> &#x767B;&#x5F55;&#x7684;&#x5BC6;&#x7801; &#x53C2;&#x6570;<span class="hljs-number">4</span> &#x9009;&#x62E9;&#x7684;&#x5E93; <span class="hljs-number">5.</span>&#x8BBE;&#x7F6E;&#x7F16;&#x7801; <span class="hljs-number">6</span>&#xFF0C;&#x8BBE;&#x7F6E;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#x683C;&#x5F0F; 

db = pymysql.connect(
    <span class="hljs-string">&apos;127.0.0.1&apos;</span>,
    <span class="hljs-string">&apos;root&apos;</span>,
    <span class="hljs-string">&apos;123456&apos;</span>,
    <span class="hljs-string">&apos;py10&apos;</span>,
    charset=<span class="hljs-string">&apos;utf8&apos;</span>,
    cursorclass=pymysql.cursors.DictCursor
    )
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../home/part2/7.慢查询及sql优化.html" class="navigation navigation-prev " aria-label="Previous page: 慢查询及sql优化"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../home/part3/1.CRUD.html" class="navigation navigation-next " aria-label="Next page: CRUD"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
